package com.biz.primus.app.rest.controller.member;

import com.biz.primus.app.rest.controller.base.RestBaseController;
import com.biz.primus.app.rest.feign.member.MemberFeignClient;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.model.member.exception.UserExceptionResponse;
import com.biz.primus.model.member.exception.UserExceptionType;
import com.biz.primus.model.member.vo.MemberInvoiceVo;
import com.biz.primus.model.member.vo.request.InvoiceTypeRequestVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人中心的发票管理相关Controller
 * @author jun.liu
 * @usage
 * @reviewer
 * @since 2017年11月3日
 */
@RestController
@RequestMapping("/users/invoice")
@Api(value = "个人中心的发票管理模块接口文档")
public class InvoiceController extends RestBaseController {

    private final MemberFeignClient memberFeignClient;

    @Autowired
    public InvoiceController(MemberFeignClient memberFeignClient) {
        this.memberFeignClient = memberFeignClient;
    }

    @ApiOperation(value = "获取用户所以发票信息", response = MemberInvoiceVo.class, responseContainer = "list")
    @GetMapping("/getInvoices")
    @UserExceptionResponse({
            UserExceptionType.MEMBERID_NULL
    })
    public JsonResult getInvoices(GlobalParams gp, @RequestBody InvoiceTypeRequestVo reqVo)  {
        reqVo = buildRequest(gp, reqVo);
        List<MemberInvoiceVo> invoices = memberFeignClient.getUserInvoices(reqVo.getInvoiceType(), reqVo.getGp().getMemberId());
        return new JsonResult(invoices);
    }

    @ApiOperation(value = "保存或者新增发票")
    @PostMapping("/save")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.MEMBERID_NULL,
            UserExceptionType.INVOICE_TITLE_NULL,UserExceptionType.INVOICE_TYPE_NULL,
            UserExceptionType.INVOICE_IDEN_NULL,UserExceptionType.USER_NOT_EXIST
    })
    public JsonResult save(GlobalParams gp, @RequestBody MemberInvoiceVo reqVo)  {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.saveUserInvoice(reqVo);
        return new JsonResult();
    }

    @ApiOperation(value = "删除发票")
    @PostMapping("/delete")
    @UserExceptionResponse({
            UserExceptionType.ID_ERROR
    })
    public JsonResult delete(GlobalParams gp, @RequestBody MemberInvoiceVo reqVo)  {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.deleteOneInvoice(reqVo.getId());
        return new JsonResult();
    }
}
